//
//  WATaskListController.m
//  WalkiOS
//
//  Created by luqiuan on 2019/10/16.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "WATaskListController.h"
#import "WATaskCell.h"
#import "WATaskModel.h"

@interface WATaskListController ()<UITextFieldDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (assign, nonatomic) NSInteger pageNum;
@property (strong, nonatomic) NSMutableArray *dataSouces;

@property (assign, nonatomic) NSInteger type;//0:任务卷轴  1：我的任务 2：历史任务
@end

@implementation WATaskListController

- (instancetype)initWtihType:(NSInteger)type
{
    self = [super init];
    if (self) {
        self.type = type;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WATaskCell" bundle:nil] forCellReuseIdentifier:@"WATaskCell"];
    
    @weakify(self)
    self.tableView.mj_header = [MJRefreshLoadHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum = 1;
        [self requestData];
    }];
    self.tableView.mj_footer = [MJRefreshMoreFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum ++;
        [self requestData];
    }];
    self.tableView.tableFooterView = [UIView new];
    [self.tableView.mj_header beginRefreshing];
}

- (void)requestData{
    [self  requestDataSucc:^(id  _Nonnull responseObject) {
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSInteger total = [dict[@"total"] integerValue];
        NSArray *memberList = [WATaskModel mj_objectArrayWithKeyValuesArray:dict[@"data"]];
        if (self.pageNum == 1) {
            self.dataSouces = [NSMutableArray new];
        }
        [self.dataSouces addObjectsFromArray:memberList];
        if(total <= self.dataSouces.count){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView.mj_header endRefreshing];
        if (self.dataSouces.count == 0) {
            self.noDataStr = @"无数据";
        }else{
            [self.tableView reloadData];
        }
        [self.tableView reloadData];
        [self.tableView reloadEmptyDataSet];
        if (self.type !=  0) {
            [self getMytaskInfoDetail];
        }
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        
    }];
}

- (void)requestDataSucc:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    if (self.type == 0) {
        [WNBusinessRequest getTaskListWithPageNum:self.pageNum Succ:succ fail:fail];
    }else if(self.type == 1){
        [WNBusinessRequest getMyTaskListWithPageNum:self.pageNum status:0 Succ:succ fail:fail];
    }else if(self.type == 2){
        [WNBusinessRequest getMyTaskListWithPageNum:self.pageNum status:1 Succ:succ fail:fail];
    }
}

- (void)getMytaskInfoDetail{
    for (int i = 0; i < self.dataSouces.count; i ++) {
        WATaskModel *model = self.dataSouces[i];
        [WNBusinessRequest checkTaskInfoWithID:[@(model.taskid) stringValue] Succ:^(id  _Nonnull responseObject) {
            WATaskModel *newMOdel = [WATaskModel mj_objectWithKeyValues:responseObject];
            [self.dataSouces replaceObjectAtIndex:i withObject:newMOdel];
            [self.tableView reloadData];
        } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
            
        }];
    }
}

#pragma mark - UITableView Delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WATaskCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WATaskCell"];
    [cell configWithModel:self.dataSouces[indexPath.row] type:self.type];
    [cell setButtonActionBlock:^(WATaskModel * _Nonnull model) {
        [self exchangeWithModel:model];
    }];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSouces.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;//高度最佳为180
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

- (void)exchangeWithModel:(WATaskModel *)model{
    [WNBusinessRequest insertMyTaskListWithIsdefault:model.isdeafault
                                              taskid:model.ID.integerValue
                                                Succ:^(id  _Nonnull responseObject) {
        [WNTool show:@"兑换成功"];
        [self.tableView.mj_header beginRefreshing];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        [WNTool show:mes];
    }];
}

#pragma mark - DZNEmptyDataSetSource
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view{
    [self.tableView.mj_header beginRefreshing];
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
    return [WNTool requestErrorWithSting:self.noDataStr];
}
@end
